package psn.hui.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.domain.Menu;
import psn.hui.sys.domain.Role;
import psn.hui.sys.mapper.RoleMapper;
import psn.hui.sys.service.RoleService;
import psn.hui.sys.utils.DataGridView;
import psn.hui.sys.vo.RoleVo;

@Service
public class RoleServiceImpl implements RoleService{
	
	@Autowired
	private RoleMapper roleMapper;
	/**
	 * 	动态sql插入角色数据
	 */
	@Override
	public int insertSelective(Role record) {
		return roleMapper.insertSelective(record);
	}
	/**
	 * 	根据角色id修改角色数据
	 */
	@Override
	public int updateByPrimaryKeySelective(Role record) {
		return roleMapper.updateByPrimaryKey(record);
	}
	/**
	 *	 根据角色id删除角色数据，并关联删除sys_role_menu和sys_role_user表的数据
	 */
	@Override
	public int deleteById(Integer id) {
		//根据角色id删除角色数据
		int rs = roleMapper.deleteByPrimaryKey(id);
		//关联删除sys_role_menu表的数据
		rs += roleMapper.deleteRoleMenuByRid(id);
		//关联删除sys_role_user表的数据
		rs += roleMapper.deleteRoleUserByRid(id);
		return rs;
	}
	/**
	 * 	条件查询角色列表
	 */
	@Override
	public List<Role> queryAllRoleForList(Role role) {
		return roleMapper.queryAllRoleForList(role);
	}
	/**
	 * 	根据角色id批量删除角色数据
	 */
	@Override
	public int batchDeleteRoleByIds(Integer[] ids) {
		int rs = 0;
		for (Integer id : ids) {
			rs += deleteById(id);
		}
		return rs;
	}
	
	
	/**
	 *	 保存分配角色菜单，先删除旧的分配记录，在添加新的
	 */
	@Override
	public int saveRoleMenu(RoleVo roleVo) {
		//先删除旧的分配记录
		int rs = roleMapper.deleteRoleMenuByRid(roleVo.getRoleid());
		//然后保存新的分配记录
		for(Integer mid : roleVo.getIds()) {
			rs += roleMapper.saveRoleMenu(roleVo.getRoleid(), mid);
		}
		return rs;
	}
	
	/**
	 * 	根据用户id查询其拥有的角色
	 */
	@Override
	public List<Role> queryRoleByUserIdForList(Integer available, Integer userId) {
		return roleMapper.queryRoleByUserIdForList(available,userId);
	}
	/**
	 * 	初始化数据表格，根据用户id选中
	 * @param userid
	 * @return
	 */
	public DataGridView initRoleTableByUserId(Integer userid) {
		Role role = new Role();
		role.setAvailable(SysConstant.ROLE_AVAILABLE_TRUE);
		List<Role> allRole = queryAllRoleForList(role);
		List<Role> userRole = queryRoleByUserIdForList(SysConstant.ROLE_AVAILABLE_TRUE, userid);
		List<Map<String, Object>>	data = new ArrayList<Map<String,Object>>();
		for (Role role1 : allRole) {
			Map<String, Object> map = new HashMap<String, Object>();
			Boolean LAY_CHECKED=false;
			for (Role role2 : userRole) {
				if(role1.getRoleid() == role2.getRoleid()) {
					LAY_CHECKED = true;
					break;
				}
			}
			map.put("roleid", role1.getRoleid());
			map.put("rolename", role1.getRolename());
			map.put("roledesc", role1.getRoledesc());
			map.put("LAY_CHECKED", LAY_CHECKED);
			data.add(map);
		}
		return new DataGridView(data);
	}

}
